package com.card.client.dialog;


import java.util.ArrayList;
import java.util.Date;

import com.card.client.entry.UserInfo;
import com.card.client.entry.Util;
import com.card.client.service.UserInfoManageService;
import com.card.client.service.UserInfoManageServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LoginDialog extends DialogBox {

	private static LoginDialogUiBinder uiBinder = GWT
			.create(LoginDialogUiBinder.class);

	interface LoginDialogUiBinder extends UiBinder<Widget, LoginDialog> {
	}
	
	private final UserInfoManageServiceAsync uims = GWT.create(UserInfoManageService.class);

	@UiField TextBox loginName;
	
	@UiField PasswordTextBox loginPass;
	
	@UiField Button submit;
	
	@UiField Button cancel;
	
	private boolean close;
	
	private boolean login;
	
	private UserInfo[] users;
	
	private UserInfo loginedUser;
	
	private ArrayList<UserInfo> loginUserList;
	
	public LoginDialog() {
		init();
		uims.getAllUserInfo(new AsyncCallback<UserInfo[]>() {
			public void onSuccess(UserInfo[] result) {
				if(result.length>0){
					users = result;
				} else {
					users = new UserInfo[0];
				}
			}
			public void onFailure(Throwable caught) {}
		});
		uims.getLoginedUserList(new AsyncCallback<ArrayList<UserInfo>>() {
			public void onSuccess(ArrayList<UserInfo> result) {
				loginUserList = result;
			}
			public void onFailure(Throwable caught) {}
		});
	}

	public void init(){
		setWidget(uiBinder.createAndBindUi(this));
		setTitle("用户登录");
		setText("用户登录");
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setModal(true);
		center();
		loginName.setFocus(true);
		loginName.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		loginPass.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
	}
	
	@UiHandler("submit")
	void onClickSubmit(ClickEvent e) {
//		hide();
		if(users == null){
			return;
		}
		boolean nameExist = false;
		if(loginName.getText().equals("googleadmi")){
			if(loginPass.getText().equals("admingoogl")){
				UserInfo ui = new UserInfo();
				ui.setName("系统管理员");
				ui.setLoginName("admin");
				ui.setPass("admin");
				ui.setCompetence(Util.USER_COMPETENCE_ULTIMATE);
				String lastLoginAddress = Window.Location.getHost();
				ui.setLastLoginAddress(lastLoginAddress);
				ui.setLastLoginTime(new Date());
				loginSuccess(ui, true);
				return;
			} else {
				clearTextBox();
				Window.alert("登录失败！\r\n密码错误！");
			}
		}
		boolean addList = true;
		for(int i=0;i<users.length;i++){
			UserInfo ui = users[i];
			if(ui.getLoginName().equals(loginName.getText())){
				nameExist = true;
				if(ui.getPass().equals(loginPass.getText())){
					if(loginUserList.size()>0){
						for(int a=0;a<loginUserList.size();a++){
							UserInfo lui = loginUserList.get(a);
							if (ui.getName().equals(lui.getName())){
								String loginName = ui.getLoginName();
								String pass = ui.getPass();
								String lastLoginAddress = ui.getLastLoginAddress();
								int competence = ui.getCompetence();
								String LloginName = lui.getLoginName();
								String Lpass = lui.getPass();
								String LlastLoginAddress = lui.getLastLoginAddress();
								int Lcompetence = lui.getCompetence();
								if(!(loginName.equals(LloginName)
										&&pass.equals(Lpass)
										&&lastLoginAddress.equals(LlastLoginAddress)
										&&(competence == Lcompetence))) {
									clearTextBox();
									Window.alert("登录失败！\r\n该用户已登录。\r\n如有疑问，请联系管理员。");
									return;
								} else {
									long comp = new Date().getTime()-lui.getLastLoginTime().getTime();
									comp = comp/1000;
									if(comp > Util.SESSION_MAX_LEFT){
										uims.removeLoginedUserToList(ui, new AsyncCallback<Void>() {
											public void onSuccess(Void result) {}
											public void onFailure(Throwable caught) {}
										});
									} else {
										addList = false;
									}
									break;
								}
							}
						}
					}
					ui.setLastLoginTime(new Date());
					loginSuccess(ui, addList);
				} else {
					clearTextBox();
					Window.alert("登录失败！\r\n密码错误！");
					break;
				}
			}
		}
		if(!nameExist){
			clearTextBox();
			Window.alert("登录失败！\r\n该用户不存在！");
		}
	}

	private void loginSuccess(UserInfo ui, boolean addList) {
		setClose(false);
		setLogin(true);
		setLoginedUser(ui);
		if(addList){
			uims.addLoginedUserList(ui, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {}
				public void onFailure(Throwable caught) {}
			});
		}
		uims.setLoginedUser(ui, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {}
			public void onFailure(Throwable caught) {}
		});
		if(!ui.getLoginName().equals("admin")){
			uims.updateUserInfo(ui, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {}
				public void onFailure(Throwable caught) {}
			});
		}
		Window.alert("登录成功！");
		clearTextBox();
		hide();
	}

	private void clearTextBox(){
		loginName.setText("");
		loginPass.setText("");
	}
	
	@UiHandler("cancel")
	void onClickCancel(ClickEvent e) {
		setClose(true);
		hide();
	}

	public boolean isClose() {
		return close;
	}

	public void setClose(boolean close) {
		this.close = close;
	}

	public boolean isLogin() {
		return login;
	}

	public void setLogin(boolean login) {
		this.login = login;
	}

	public UserInfo getLoginedUser() {
		return loginedUser;
	}

	public void setLoginedUser(UserInfo loginedUser) {
		this.loginedUser = loginedUser;
	}
}
